using System;
using System.ComponentModel.Design;
using System.Drawing;
using EnvDTE;
using VSSDKAssist.Assets.Helpers;

namespace VSSDKAssist.Recipes.Common.Helpers
{
	/// <summary>
	/// Custom Pages Helper class
	/// </summary>
	public static class CustomPageHelper
	{
        /// <summary>
		/// Projects the item exists.
		/// </summary>
		/// <param name="dictionaryService">The dictionary service.</param>
		/// <param name="projectArgument">The project argument.</param>
		/// <param name="folderArgument">The folder argument.</param>
		/// <param name="itemName">Name of the item.</param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
		public static bool ProjectItemExists(IDictionaryService dictionaryService, string projectArgument, string folderArgument, string itemName)
		{
			Guard.ArgumentNotNull(dictionaryService, "dictionaryService");
			Guard.ArgumentNotNullOrEmptyString(projectArgument, "projectArgument");
			Guard.ArgumentNotNullOrEmptyString(folderArgument, "folderArgument");
			Guard.ArgumentNotNullOrEmptyString(itemName, "itemName");

			Project project = dictionaryService.GetValue(projectArgument) as Project;

			if(project != null)
			{
				if(!String.IsNullOrEmpty(folderArgument))
				{
                    if(dictionaryService.GetValue(folderArgument) is ProjectItem)
                    {
                        ProjectItem folder = dictionaryService.GetValue(folderArgument) as ProjectItem;

                        if(VSIPHelper.FindItemByName(folder.ProjectItems, itemName, true) != null)
                        {
                            return true;
                        }
                    }
                    else
                    {
                        string folderName = dictionaryService.GetValue(folderArgument) as string;

                        if(!String.IsNullOrEmpty(folderName))
                        {
                            ProjectItem folder = VSIPHelper.FindItemByName(project.ProjectItems, folderName, true);

                            if(folder != null)
                            {
                                if(VSIPHelper.FindItemByName(folder.ProjectItems, itemName, true) != null)
                                {
                                    return true;
                                }
                            }
                        }
                    }
				}
				else
				{
					if(VSIPHelper.FindItemByName(project.ProjectItems, itemName, true) != null)
					{
						return true;
					}
				}
			}

			return false;
		}

		/// <summary>
		/// Gets the bitmap resource.
		/// </summary>
		/// <param name="resourceName">Name of the resource.</param>
		/// <returns></returns>
		public static Bitmap GetBitmapResource(string resourceName)
		{
			Bitmap bitmap = Properties.Resources.ResourceManager.GetObject(resourceName) as Bitmap;

			return bitmap;
		}
	}
}